#!/bin/bash

# Back up tomcat8 config file before changing it
cp /etc/default/tomcat8 /etc/default/tomcat8.orig

#GeoServer needs more ram than the default for tomcat.
JVM_OPTS='JAVA_OPTS="-Djava.awt.headless=true -Xms2048m -Xmx2048m -XX:+UseParallelOldGC -XX:+UseParallelGC -XX:NewRatio=2 -XX:+AggressiveOpts -Xrs -XX:PerfDataSamplingInterval=500 -XX:MaxPermSize=128m"'

# Append a line with the new jvm configuration
if [ "$(grep ^GEOSERVER /etc/default/tomcat8)" == "" ]; then
	echo '# GEOSERVER additions' >> /etc/default/tomcat8
	echo 'JAVA_HOME=/usr/' >> /etc/default/tomcat8
	echo $JVM_OPTS >> /etc/default/tomcat8
    sed -i -e 's/xom-\*\.jar/xom-\*\.jar,bcprov\*\.jar/g' /var/lib/tomcat8/conf/catalina.properties
    mv /etc/tomcat8/tomcat-users.xml /etc/tomcat8/tomcat-users.xml.org
    echo '<tomcat-users xmlns="http://tomcat.apache.org/xml"' >> /etc/tomcat8/tomcat-users.xml
    echo '          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' >> /etc/tomcat8/tomcat-users.xml
    echo '          xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"' >> /etc/tomcat8/tomcat-users.xml
    echo '          version="1.0">' >> /etc/tomcat8/tomcat-users.xml
    echo '    <role rolename="tomcat"/>' >> /etc/tomcat8/tomcat-users.xml
    echo '    <role rolename="manager-gui"/>' >> /etc/tomcat8/tomcat-users.xml
    echo '    <user username="tomcat" password="tomcat" roles="tomcat,manager-gui"/>' >> /etc/tomcat8/tomcat-users.xml
    echo '</tomcat-users>' >> /etc/tomcat8/tomcat-users.xml
fi

# Fix permissions on deployed jar
chown -R tomcat8:tomcat8 /usr/share/geoserver/

# start tomcat after installing geoserver
service tomcat8 restart
